package com.customization.shunsheng.util;

import com.api.browser.util.BrowserConfigComInfo;
import com.api.browser.util.BrowserConstant;
import com.weaver.formmodel.util.StringHelper;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.filter.XssUtil;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.general.Util;
import weaver.interfaces.workflow.browser.Browser;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.field.BrowserComInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeaveTypeBrowseButtonUtil {

    public static final String BROWSER = "BROWSER";
    public static BaseBean baseBean = new BaseBean();
    public static String broswerTypes = BrowserConstant.MULT_BROWSER_TYPE;//多选的
    public static String broswerTypes1 = ",274,25,";//没有高级搜索的

    public static Map<String, Object> handleTaskTempletFormCmd(String viewtype, Map<String, Object> params, Map<String, Object> result) {
        int viewAttr = 1;
        if ("edit".equals(viewtype) || "add".equals(viewtype)) {
            viewAttr = 2;
        }
        String typeId = Util.null2String(params.get("typeId"));
        String resourceIds = "";
        RecordSet rs = new RecordSet();
        if (StringUtil.isNotNull(typeId)) {
            String sql1 = "select ygxm from uf_jqlxb where isdelete = 1 and jqlx= " + typeId;
            rs.execute(sql1);
            while (rs.next()) {
                if (StringUtil.isNotNull(resourceIds)) {
                    resourceIds = resourceIds + ",";
                }
                resourceIds = resourceIds + Util.null2String(rs.getString("ygxm"));
            }
        }
        String leaveIds = "";
        String getLeaveTypeSql = "select corresLeaveType from kq_LeaveRules where id = "+typeId;
        rs.executeQuery(getLeaveTypeSql);
        while (rs.next()) {
            if (StringUtil.isNotNull(leaveIds)) {
                leaveIds = leaveIds + ",";
            }
            leaveIds = leaveIds + Util.null2String(rs.getString("corresLeaveType"));
        }

        List<Map<String, Object>> conditions = (List<Map<String, Object>>) result.get("condition");
        Map<String, Object> condition = conditions.get(0);
        List<Object> items = (List<Object>) condition.get("items");
        items.add(4, LeaveTypeBrowseButtonUtil.getFormItemForBrowser("hrmResourceId", "员工", "17", resourceIds, viewAttr, "", null, null));
        items.add(12, LeaveTypeBrowseButtonUtil.getFormItemForBrowser("leaveType", "假期类型", "leaveType", leaveIds, viewAttr, "", null, null));

        return result;
    }

    /**
     * 防止强转出现警告
     * @param obj   强转对象
     * @param clazz 强转类型内含类型
     * @param <T>   泛型
     * @return
     */
    public static <T> List<T> castList(Object obj, Class<T> clazz) {
        List<T> result = new ArrayList<T>();
        if (obj instanceof List<?>) {
            for (Object o : (List<?>) obj) {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }

    public static Map<String, Object> getFormItemForBrowser(String fieldName, String fieldLabel, String browserType, String value, int viewAttr, String dmlUrl, String linkUrl, Map<String, Object> dataParams) {

        RecordSet rs = new RecordSet();
        Map<String, Object> formItem = new HashMap<String, Object>();
        //浏览框URL
        BrowserConfigComInfo browserConfigComInfo = new BrowserConfigComInfo();
        BrowserComInfo browserComInfo = new BrowserComInfo();
        linkUrl = Util.null2s(linkUrl,"");
        if ("".equals(linkUrl)) {
            linkUrl = Util.null2String(browserConfigComInfo.getLinkurl(browserType));
            if (Util.getIntValue(browserType, 0) > 0) {
                if ("".equals(linkUrl)) {
                    linkUrl = Util.null2String(browserComInfo.getLinkurl(browserType));
                }
            }
            if (browserType.equals("161")||browserType.equals("162")||browserType.equals("256") || browserType.equals("257")) {
                try {
                    Browser browser = (Browser) StaticObj.getServiceByFullname(dmlUrl, Browser.class);
                    linkUrl  = browser.getHref();
                } catch (Exception e) {
                    baseBean.writeLog(e);
                }
            }
        }
        List<String> domKeyList = new ArrayList<String>();
        domKeyList.add(fieldName);

        Map<String, Object> browserConditionParam = new HashMap<String, Object>();
        List<Map<String, String>> replaceDatas = new ArrayList<Map<String, String>>();
        List<Map<String, String>> replaceDatas1 = new ArrayList<Map<String, String>>();
        value = Util.null2String(value);
        String ids[] = value.split(",");
        List <String> _ids =  new ArrayList<String>();
        for(int i=0;i<ids.length;i++){
            if(!"".equals(ids[i])){
                _ids.add(ids[i].trim());
            }
        }
        value = StringHelper.List2String(_ids, ",");
        if (!"".equals(value)) {
            String showName = "";
            if (dm.jdbc.util.StringUtil.equals(browserType, "leaveType")) {
                String sql = "SELECT leaveName FROM kq_LeaveRules where id = " + value;
                rs.execute(sql);
                if (rs.next()) {
                    showName = rs.getString("leaveName");
                }
            }
            if ("17".equals(browserType)) {
                String sql = "SELECT lastname FROM hrmresource where id in ( " + value+" )";
                rs.execute(sql);
                while (rs.next()) {
                    if (StringUtil.isNotNull(showName)){
                        showName=showName+",";
                    }
                    showName = showName + rs.getString("lastname");
                }
            }
            if (broswerTypes.indexOf("," + browserType + ",") > -1){
                //String ids[] = value.split(",");
                String showNames[] = showName.split(",");
                for(int i = 0;i<_ids.size();i++){
                    Map<String, String> browserOptionMap = new HashMap<String, String>(2);
                    Map<String, String> browserOptionMap1 = new HashMap<String, String>(2);
                    browserOptionMap.put("id", _ids.get(i));
                    browserOptionMap1.put("id", _ids.get(i));
                    if(showNames.length>i){
                        browserOptionMap.put("name", showNames[i]);
                        browserOptionMap1.put("name", showNames[i]);
                    }else{
                        browserOptionMap.put("name", "");
                        browserOptionMap1.put("name", "");
                    }
                    replaceDatas.add(browserOptionMap);
                    replaceDatas1.add(browserOptionMap1);
                }
            }else{
                Map<String, String> browserOptionMap = new HashMap<String, String>(2);
                Map<String, String> browserOptionMap1 = new HashMap<String, String>(2);
                browserOptionMap.put("id", value);
                browserOptionMap.put("name", showName);
                replaceDatas.add(browserOptionMap);

                browserOptionMap1.put("id", value);
                browserOptionMap1.put("name", showName);
                replaceDatas1.add(browserOptionMap1);
            }

            browserConditionParam.put("replaceDatas", replaceDatas);
        }

        if (null == dataParams) {
            dataParams = new HashMap<String, Object>();
        }
        //资产资料，不显示链接
        if("179".equals(browserType)||"314".equals(browserType)){
            XssUtil xssUtil = new XssUtil();
            dataParams.put("sqlwhere", xssUtil.put(" isdata='1' "));
        }

        browserConditionParam.put("dataParams", dataParams);
        browserConditionParam.put("hasAdd", false);
        if (broswerTypes1.indexOf("," + browserType + ",") > -1) {
            browserConditionParam.put("hasAdvanceSerach", false);
        } else {
            browserConditionParam.put("hasAdvanceSerach", true);
        }
        browserConditionParam.put("isAutoComplete", 1);
        browserConditionParam.put("isDetail", 0);
        if(browserType.equals("162")||browserType.equals("257")){
            browserConditionParam.put("isMultCheckbox", true);
        }else{
            browserConditionParam.put("isMultCheckbox", false);
        }

        browserConditionParam.put("isSingle", true);
        browserConditionParam.put("linkUrl", linkUrl);
        browserConditionParam.put("title", fieldLabel);
        browserConditionParam.put("type", browserType);
        browserConditionParam.put("viewAttr", viewAttr);
        // 如果是部门或者分部，增加tab参数
        if (browserType.equals("4") || browserType.equals("164")) {
            List<Map<String, Object>> tabList = new ArrayList<Map<String, Object>>();
            Map<String, Object> tabMap1 = new HashMap<String, Object>();
            Map<String, Object> tabMap1Sub = new HashMap<String, Object>();
            tabMap1Sub.put("list", "1");
            tabMap1.put("dataParams", tabMap1Sub);
            tabMap1.put("key", "1");
            tabMap1.put("name", SystemEnv.getHtmlLabelName(130619,7));
            tabMap1.put("selected", false);
            tabList.add(tabMap1);
            Map<String, Object> tabMap2 = new HashMap<String, Object>();
            tabMap2.put("key", "2");
            tabMap2.put("name", SystemEnv.getHtmlLabelName(18770,7));
            tabMap2.put("selected", false);
            tabList.add(tabMap2);
            browserConditionParam.put("tabs", tabList);
        }
        if (browserType.equals("161") || browserType.equals("162")||browserType.equals("256") || browserType.equals("257")) {
            dataParams.put("type", dmlUrl);
            dataParams.put("mouldID", "prj");
            dataParams.put("selectedids", "");
            browserConditionParam.put("dataParams", dataParams);
            dataParams = new HashMap<String, Object>();
            dataParams.put("type", dmlUrl);
            dataParams.put("fielddbtype", dmlUrl);
            dataParams.put("mouldID", "prj");
            dataParams.put("selectedids", "");
            browserConditionParam.put("conditionDataParams", dataParams);
            dataParams = new HashMap<String, Object>();
            dataParams.put("type", dmlUrl);
            dataParams.put("fielddbtype", dmlUrl);
            dataParams.put("mouldID", "prj");
            dataParams.put("selectedids", value);
            browserConditionParam.put("destDataParams", dataParams);
            dataParams = new HashMap<String, Object>();
            dataParams.put("type", dmlUrl);
            dataParams.put("fielddbtype", dmlUrl);
            dataParams.put("mouldID", "prj");
            dataParams.put("selectedids", "");
            browserConditionParam.put("completeParams", dataParams);
        }
        if (browserType.equals("prjtsk")) {
            String prjid = Util.null2String(dataParams.get("prjid"));
            Map<String,String> m = new HashMap<String,String>();
            m.put("prjid", prjid);
            browserConditionParam.put("conditionDataParams", m);
            m = new HashMap<String,String>();
            m.put("prjid", prjid);
            browserConditionParam.put("completeParams", m);
        }
        if (browserType.equals("179")||browserType.equals("23") || browserType.equals("26") || browserType.equals("3") || browserType.equals("7")|| browserType.equals("314") || browserType.equals("315")) {
            Map<String, Object> conditionDataParams = new HashMap<String, Object>();
            Map<String, Object> completeParams = new HashMap<String, Object>();
            Map<String, Object> destDataParams = new HashMap<String, Object>();
            for(String key : dataParams.keySet()){
                conditionDataParams.put(key, dataParams.get(key));
                completeParams.put(key, dataParams.get(key));
                destDataParams.put(key, dataParams.get(key));
            }
            browserConditionParam.put("conditionDataParams", conditionDataParams);
            browserConditionParam.put("destDataParams", destDataParams);
            browserConditionParam.put("completeParams", completeParams);
        }

        if (broswerTypes.indexOf("," + browserType + ",") > -1) {
            browserConditionParam.put("isSingle", false);
            if(browserType.equals("314") || browserType.equals("315")){
                dataParams.put("isMultBrow","1");
            }
        } else {
            browserConditionParam.put("isSingle", true);
        }
        //资产、资产资料浏览框树形
        if(browserType.equals("179")||browserType.equals("23")){
            Map<String, Object> modalStyle = new HashMap<String,Object>();
            modalStyle.put("width", 988);
            browserConditionParam.put("modalStyle", modalStyle);
            browserConditionParam.put("hasLeftTree", true);
            browserConditionParam.put("leftToSearchKey", "capitalgroupid");
        }
        formItem.put("browserConditionParam", browserConditionParam);
        formItem.put("label", fieldLabel);
        formItem.put("colSpan", 2);
        formItem.put("labelcol", 5);
        formItem.put("fieldcol", 14);
        formItem.put("domkey", domKeyList);
        formItem.put("conditionType", BROWSER);
        formItem.put("value", value);
        formItem.put("browserType", browserType);

        Map<String, Object> otherParams = new HashMap<String, Object>();
        otherParams.put("replaceDatas", replaceDatas1);
//        formItem.put("otherParams", otherParams);
        if(viewAttr == 3){
            formItem.put("rules", "required");
        }
        return formItem;
    }
}
